package com.sright.bcard.content.biz.manager.impl;

import com.sright.bcard.common.service.share.dto.LoginUser;
import com.sright.bcard.common.service.share.exception.base.BusinessErrorException;
import com.sright.bcard.common.service.share.exception.code.BusinessCode;
import com.sright.bcard.common.service.share.utils.DateTimeUtils;
import com.sright.bcard.content.biz.manager.TagManager;
import com.sright.bcard.content.dal.dao.TagLibraryDAO;
import com.sright.bcard.content.dal.entity.TagLibraryDO;
import com.sright.bcard.common.service.share.dto.content.TagDTO;
import com.sright.bcard.content.share.service.UserTagService;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.transaction.interceptor.TransactionAspectSupport;
import org.springframework.util.StringUtils;

import java.util.ArrayList;
import java.util.List;

/**
 * @author peter
 * @description
 * @date 2020/6/4
 */
@Service
public class TagManagerImpl implements TagManager {

    @Autowired
    private TagLibraryDAO tagLibraryDAO;

    @Autowired
    private UserTagService userTagService;

    @Override
    public Long addTag(String name, LoginUser loginUser) {
        if (StringUtils.isEmpty(name)) {
            return 0L;
        }

        Long enterpriseId = loginUser.getEnterpriseId();
        if (enterpriseId == null) {
            return 0L;
        }

        TagLibraryDO tagLibraryDO = new TagLibraryDO();
        tagLibraryDO.setName(name);
        tagLibraryDO.setEnterpriseId(Long.parseLong(enterpriseId.toString()));
        tagLibraryDO.setCreatedAt(DateTimeUtils.getNowTime());
        tagLibraryDO.setUpdatedAt(DateTimeUtils.getNowTime());

        return tagLibraryDAO.insert(tagLibraryDO);
    }

    @Override
    @Transactional(rollbackFor = Throwable.class)
    public Boolean removeTag(Long id, LoginUser loginUser) {
        if (id == null) {
            return false;
        }

        Long enterpriseId = loginUser.getEnterpriseId();
        if (enterpriseId == null) {
            return false;
        }

        try {
            int ret = tagLibraryDAO.deleteByPrimaryKeyAndEnterpriseId(id, enterpriseId);
            if (ret == 0) {
                throw new BusinessErrorException(BusinessCode.SYS_ERROR);
            }

            if (!userTagService.removeUserTag(id)) {
                throw new BusinessErrorException(BusinessCode.SYS_ERROR);
            }

            return true;
        } catch (Exception exception) {
            TransactionAspectSupport.currentTransactionStatus().setRollbackOnly();
            return false;
        }
    }

    @Override
    public List<TagDTO> listTag(String name, LoginUser loginUser) {
        Long enterpriseId = loginUser.getEnterpriseId();
        if (enterpriseId == null) {
            return null;
        }

        List<TagLibraryDO> list = tagLibraryDAO.selectTagsLikeName(enterpriseId, name);
        List<TagDTO> dtoList = new ArrayList<>();
        for (TagLibraryDO tag : list) {
            TagDTO tagDTO = new TagDTO();
            BeanUtils.copyProperties(tag, tagDTO);
            dtoList.add(tagDTO);
        }

        return dtoList;
    }
}
